package Client.GUI;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import Client.logic.ClientIF;
import Server.DataBase.Lesson;
import Server.DataBase.Student;
import Server.Message.MessageGetAllStudents;
import Server.Message.MessageGetStudentLesson;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageLessonReplay;
import Server.Message.MessageSendRequest;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class RemoveStudentFromCoursePanel extends MyJPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox studentcomboBox;
	private JComboBox lessonComboBox;
	private JButton btnReset;
	private JButton btnSendRequestTo;
	private ArrayList<Student> allStudentArray =null;
	private ArrayList<Lesson> lessonArray =null;
	private Student student;
	private Lesson lesson;
	
	public RemoveStudentFromCoursePanel( ClientIF client) {
		super(PanelType.REMOVE_STUDENT_FROM_COURSE_PANEL, client);
		setLayout(null);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"remove student from course.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(12, 13, 699, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		init();

	}
	public void initComboBoxs(){
		
		studentcomboBox = new JComboBox();
		studentcomboBox.setBounds(188, 143, 108, 22);
		add(studentcomboBox);
		
		lessonComboBox = new JComboBox();
		lessonComboBox.setBounds(188, 223, 108, 22);
		add(lessonComboBox);
		lessonComboBox.setEnabled(false);
	}
	
	public void initButtons(){
		btnReset = new JButton("reset");
		btnReset.setBounds(188, 307, 97, 25);
		add(btnReset);
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().swapFromBack(pushPanel());
			}
		});
		btnReset.setEnabled(false);
		
		btnSendRequestTo = new JButton("Send request to manager");
		btnSendRequestTo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().sendMsgToServer(new MessageSendRequest(2, student.getIdUser(), lesson.getIdLesson()));
				JOptionPane.showMessageDialog((Component) getClient(), "Request has been send");
				getClient().swapFromBack(pushPanel());
			}
		});
		btnSendRequestTo.setBounds(159, 392, 209, 25);
		add(btnSendRequestTo);
		btnSendRequestTo.setEnabled(false);
	}
		
	public void initArrays(){
		allStudentArray = new ArrayList<Student>();
		getClient().sendMsgToServer(new MessageGetAllStudents());
		MessageGetStudentsReplay rep = (MessageGetStudentsReplay) getClient().getMessageFromServer();
		allStudentArray = rep.getArray();
	}

	public void init(){
		initArrays();
		initComboBoxs();
		initButtons();
		
		JLabel lblStudent = new JLabel("student:");
		lblStudent.setBounds(82, 146, 70, 16);
		add(lblStudent);
		
		JLabel lblCourse = new JLabel("course:");
		lblCourse.setBounds(82, 226, 56, 16);
		add(lblCourse);
		
		for (int i=0; i<=allStudentArray.size(); i++){
			if (i==0) studentcomboBox.addItem("Choose..");
			else studentcomboBox.addItem(allStudentArray.get(i-1));
		}
		
		studentcomboBox.addActionListener(new StudentListener());
	}

	private class StudentListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			student = (Student) studentcomboBox.getSelectedItem();
			
			getClient().sendMsgToServer(new MessageGetStudentLesson(student.getIdUser()));
			MessageLessonReplay msg2 = (MessageLessonReplay) getClient().getMessageFromServer();
			lessonArray = msg2.getArray();
			
			for (int i=0; i<=lessonArray.size(); i++){
				if (i==0) lessonComboBox.addItem("Choose..");
				else lessonComboBox.addItem(lessonArray.get(i-1));
			}
			lessonComboBox.setEnabled(true);
			studentcomboBox.setEnabled(false);
			btnReset.setEnabled(true);
			lessonComboBox.addActionListener(new LessonListener());
		}
	}
	private class LessonListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			lesson = (Lesson) lessonComboBox.getSelectedItem();
			btnSendRequestTo.setEnabled(true);
			btnReset.setEnabled(true);
		}	
	}
	
	public MyJPanel pushPanel() {
		return new RemoveStudentFromCoursePanel(getClient());

	}

}
